<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="index" label="题目序号">
				<uni-easyinput placeholder="题目序号" type="number" v-model="formData.index"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="belong_hd" label="活动名称">
				<uni-data-picker v-model="formData.belong_hd" collection="abu-answering-activity"
					field="title as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="topic" label="题干">
				<uni-easyinput placeholder="题干" v-model="formData.topic" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="score" label="分数">
				<uni-easyinput placeholder="分数" v-model="formData.score" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="correct" label="正确答案">
				<uni-easyinput placeholder="正确答案" v-model="formData.correct" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="errorRes" label="错误反馈">
				<uni-easyinput placeholder="错误反馈" v-model="formData.errorRes" trim="both"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/abu-question-info.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'abu-question-info';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"index": null,
				"belong_hd": "",
				"topic": "",
				"score": "",
				"correct": "",
				"errorRes": ""
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field("index,belong_hd,topic,score,correct,errorRes").get().then((
					res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>